package ysq.xyg.host.controller;

import cn.hutool.core.collection.CollUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.model.po.HostItemSupplement;
import ysq.xyg.host.model.vo.HostItemSupplementVo;
import ysq.xyg.host.service.HostItemService;
import ysq.xyg.host.service.HostItemSupplementService;
import ysq.xyg.sys.model.po.SysUser;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 房产-楼盘补充信息controller层
 *
 * @author congcong tao
 * @date 2019-12-11 15:17:59
 */
@RestController
public class HostItemSupplementController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(HostItemSupplementController.class);

    @Autowired
    private HostItemSupplementService hostItemSupplementService;
    @Autowired
    private HostItemService hostItemService;

    @RequestMapping(value = "/hostItemSupplement/getPage", method = RequestMethod.GET)
    public ModelAndView getPage(String itemId) {
        ModelAndView view = new ModelAndView("/host/hostItemSupplementList");
        view.addObject("item", hostItemService.selectByPrimaryKey(itemId));
        return view;
    }

    /**
     * 分页查询
     *
     * @param hostItemSupplementVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @BaseLog(module = "房产", className = "楼盘补充信息",methods = "分页查询", businessType = BaseConstant.SYS_BUSINESS_TYPE_GET, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItemSupplement/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request, HostItemSupplementVo hostItemSupplementVo, int pageSize, int pageNum){
        SysUser sysUser = acceptCurrentUser(request);
        hostItemSupplementVo.setAccountId(sysUser.getAccountId());
        ReturnEntity returnEntity = hostItemSupplementService.page(hostItemSupplementVo, pageSize, pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param itemId 楼盘主键id
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @RequestMapping(value = "/hostItemSupplement/getEntity", method = RequestMethod.GET)
    public ModelAndView getEntity(String itemId) {
        ModelAndView view = new ModelAndView("/host/hostItemSupplementUpdateOrSave");
        HostItemSupplementVo hostItemSupplementVo = new HostItemSupplementVo();
        hostItemSupplementVo.setItemId(itemId);
        List<HostItemSupplement> hostItemSupplements = hostItemSupplementService.acceptList(hostItemSupplementVo);
        if (CollUtil.isNotEmpty(hostItemSupplements)) {
            view.addObject("hostItemSupplement", hostItemSupplements.get(0));
        } else {
            view.addObject("hostItemSupplement", new HostItemSupplement());
        }
        view.addObject("itemId", itemId);
        view.addObject("item", hostItemService.selectByPrimaryKey(itemId));
        return view;
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @BaseLog(module = "房产", className = "楼盘补充信息", methods = "删除", businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItemSupplement/delete")
    public ReturnEntity delete(HttpServletRequest request, String id){
        return hostItemSupplementService.delete(id, acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param hostItemSupplementVo Vo对象
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @BaseLog(module = "房产", className = "楼盘补充信息", methods = "添加或修改", businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItemSupplement/addOrUpdate", method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request, HostItemSupplementVo hostItemSupplementVo){
        return hostItemSupplementService.addOrUpdate(hostItemSupplementVo, acceptCurrentUser(request));
    }
}